function main(defaultParams) {
    const self = this;
    debugger
    const formPartEle = self.$$element("form_part_f4uke4");
    const formEle = formPartEle.$$getRenderElement();
    const pageRenderElement = this.$$root().$parent.$parent.$parent;

    let extraParams = self.state.extraParams;

    if (formPartEle) {
        formEle.$$validateData().then((res) => {
            if (!res.success) {
                var target = formEle.state.validate.target;
                return;
            } else {
                const formValue = formEle.$$getFormValue()['ds_tbl:t_355f38575f894287a8e40d8af41ae10c'];
                const unitTypeCode = formValue.checkbox_davb9x || []; // 多选机构类型

                const dataForSave = buildUnitInfo(formEle, formValue, unitTypeCode, extraParams);
                console.log("构建后的数据：", dataForSave);

                saveForm(dataForSave).then(res => {
                    console.log(res);
                    let hasError = JSON.parse(res.result).hasError
                    if (hasError) {
                        return;
                    }
                    pageRenderElement.goHistory(-1); // 保存成功后返回
                });
            }
        });
    }


}

function saveForm(data) {
    return api.DATA_SOURCE_EXECUTE(
        "ds_api:8222f7d2-792a-408d-9b80-2e6ed3160030",
        "api",
        [],
        [],
        JSON.stringify(data)
    );
}

function buildUnitInfo(formEle, formValue, unitTypeCode, extraParams) {
    const id = formValue && formValue.id;

    const unitBaseInfo = {
        id,
        unitName: formValue.text_input_5dms7g,
        unitNameAbbr: formValue.text_input_67iog4,
        seniorId: formValue.tree_single_select_4rwjmn,
        unitPropertyCode: formValue.single_select_hhkn4k,
        regionId: formValue.tree_single_select_5pracq,
        regionName: formValue.tree_single_select_5pracq_hidden_field_4acpcv,
        unitTypeCode: (unitTypeCode || []).join(","),
        socialCreditCode: formValue.text_input_9ahk2x,
        unitLayerLevelCode: formValue.single_select_easbph,
        legalDelegateMan: formValue.text_input_13csj5,
        foundDate: formValue.date_picker_9g98wq,
        postalCode: formValue.text_input_5qekvi,
        email: formValue.text_input_37xmcy,
        fax: formValue.text_input_aoi7tm,
        phone: formValue.text_input_77xzz5,
        address: formValue.textarea_4faevr
    };

    // 各种类型信息结构体
    const typeHandlers = {
        "1": {
            target: "superviseUnitInfoList", getData: () => ({
                id: extraParams.jdlx_id,
                unitPropertyCode: formValue.single_select_buf1s5
            })
        },
        "2": {
            target: "superviseUnitInfoList", getData: () => ({
                id: extraParams.jdjg_id,
                unitPropertyCode: formValue.single_select_4fdqqa,
                superviseUnitId: formValue.tree_single_select_1feg0s
            })
        },
        "3": {
            target: "superviseUnitInfoList", getData: () => ({
                id: extraParams.jdlx_id,
                unitPropertyCode: formValue.single_select_buf1s5
            })
        },
        "4": {
            target: "superviseUnitInfoList", getData: () => ({
                id: extraParams.jdjg_id,
                unitPropertyCode: formValue.single_select_4fdqqa,
                superviseUnitId: formValue.tree_single_select_1feg0s
            })
        }
    };

    // 最终构建数据对象
    const result = {
        unitBaseInfo,
        superviseUnitInfoList: [],
        subjectUnitInfoList: [],
        delegationUnitInfoList: [],
        unitRelBizUnitInfoList: []
    };

    // 根据勾选的机构类型收集信息
    [...new Set(unitTypeCode)].forEach(code => {
        const handler = typeHandlers[code];
        if (handler) {
            const data = handler.getData();
            if (Array.isArray(data)) {
                result[handler.target].push(...data);
            } else if (data) {
                result[handler.target].push(data);
            }
        }
    });
    const unitTypeSet = new Set(unitTypeCode);
    const has13 = unitTypeSet.has("1") && unitTypeSet.has("3");
    const has24 = unitTypeSet.has("2") && unitTypeSet.has("4");

    const has5 = unitTypeCode.includes("5");
    const has6 = unitTypeCode.includes("6");
    const has7 = unitTypeCode.includes("7");


    if ((has13 || has24) && result.superviseUnitInfoList.length > 1) {
        const seenKeys = new Set();
        result.superviseUnitInfoList = result.superviseUnitInfoList.filter(item => {
            const key = JSON.stringify(item);
            if (seenKeys.has(key)) return false;
            seenKeys.add(key);
            return true;
        });
    }

    return result;
}
